package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionAgreeItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
//소독동의서 쓰레드
public class DisinfectionAgreeThread extends BasicThread {

	public interface DisinfectionAgreeThreadCallback {
		public void OnDisinfectionAgreeCompleted(boolean isNetworkConntected
				, DisinfectionAgreeItem quarantineItem
				, String message);
		public void OnDisinfectionAgreeError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionAgreeThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String recNo;
	private String offCd;

	public DisinfectionAgreeThread(Context context
			, DisinfectionAgreeThreadCallback callback
			, String offCd
			, String recNo
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.recNo = recNo;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String,String>();
		String param = "";
		recNo = recNo.substring(3);
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ023, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
		
	}
	
	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionAgreeCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionAgreeError(null);
				return;
			}

			try {		
				int result = json.getInt(Const.RESULT);
				
				if(result == 0){	// result 값이 0일 경우
					String data = json.getString(Const.DATA);
					
					ErrorItem errorItem = new ErrorItem();
					errorItem.setrDesc(data);

					callback.OnDisinfectionAgreeError(errorItem);
				}else{	// result 값이 0이 아닐 경우
					JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
					JSONObject disinfection = arraydisinfection.getJSONObject(0);
					
					DisinfectionAgreeItem disinfectionItem = new DisinfectionAgreeItem();
									
					disinfectionItem.setAllInsttNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.ALLINSTTNM)));
					disinfectionItem.setOffCd(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.OFFCD)));
					disinfectionItem.setRecNo(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.RECNO)));
					disinfectionItem.setRceptDate(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.RCEPTDATE)));
					disinfectionItem.setVenNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.VENNM)));
					disinfectionItem.setVenRep(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.VENREP)));
					disinfectionItem.setVenRnCode(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.VENRNCODE)));
					disinfectionItem.setVenRnAdres(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.VENRNADRES)));
					disinfectionItem.setVenRnAdresDetail(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.VENRNADRESDETAIL)));
					disinfectionItem.setTraDe(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.TRADE)));
					disinfectionItem.setNatNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.NATNM)));
					disinfectionItem.setItmNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.ITMNM)));
					disinfectionItem.setItmQty(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.ITMQTY)));
					disinfectionItem.setDikind1Nm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.DIKIND1NM)));
					disinfectionItem.setDikind2Nm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.DIKIND2NM)));
					disinfectionItem.setPreDe1(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.PREDE1)));
					disinfectionItem.setPreDe2(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.PREDE2)));
					disinfectionItem.setPreTime(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.PRETIME)));
					disinfectionItem.setDiplace(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.DIPLACE)));
					disinfectionItem.setMedNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.MEDNM)));
					disinfectionItem.setMedVol(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.MEDVOL)));
					disinfectionItem.setSanghos(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.SANGHOS)));
					disinfectionItem.setNames(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.NAMES)));
					disinfectionItem.setJobTime(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.JOBTIME)));
					disinfectionItem.setJobMin(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.JOBMIN)));
					disinfectionItem.setMedQty(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionAgreeItem.MEDQTY)));
					
					callback.OnDisinfectionAgreeCompleted(true, disinfectionItem, mResult);
				}
				
			} catch (JSONException e) {
				
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		
		
		
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		
		recNo = recNo.substring(3);
		post =  ParameterUtil.getDisinfectionPopupParams(offCd, recNo, 1);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
		
	}
}
